package me.aartikov.alligator.helpers;


import me.aartikov.alligator.AbilityStack;
import me.aartikov.alligator.animations.TransitionAnimation;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;

/**
 * Helper class for starting and finishing an activity with animation.
 */
public class AbilityHelper {
    private Ability abilitySlice;

    public AbilityHelper(Ability abilitySlice) {
        this.abilitySlice = abilitySlice;
    }

    public boolean resolve(Intent intent) {
//        return intent.resolveActivity(mActivity.getPackageManager()) != null;
        //TODO 暂时未找到相应的方式
        return true;
    }

    public void start(Intent intent, TransitionAnimation animation) {
//		Bundle optionsBundle = animation.getActivityOptionsBundle(mActivity);
//		animation.applyBeforeActivityStarted(mActivity, intent);
//		ActivityCompat.startActivity(mActivity, intent, optionsBundle);
//		animation.applyAfterActivityStarted(mActivity);

        if (animation != null) {
            Operation optionsBundle = animation.getActivityOptionsBundle(abilitySlice);
            animation.applyBeforeActivityStarted(abilitySlice, intent);
            intent.setOperation(optionsBundle);
        }
        abilitySlice.startAbility(intent);
        if (animation != null) {
            animation.applyAfterActivityStarted(abilitySlice);
        }
    }

    public void startForResult(Intent intent, int requestCode, TransitionAnimation animation) {
//		Bundle optionsBundle = animation.getActivityOptionsBundle(mActivity);
//		animation.applyBeforeActivityStarted(mActivity, intent);
//		ActivityCompat.startActivityForResult(mActivity, intent, requestCode, optionsBundle);
//		animation.applyAfterActivityStarted(mActivity);

        if (animation != null) {
            Operation optionsBundle = animation.getActivityOptionsBundle(abilitySlice);
            animation.applyBeforeActivityStarted(abilitySlice, intent);
            intent.setOperation(optionsBundle);
        }
        abilitySlice.startAbilityForResult(intent, requestCode);
        if (animation != null) {
            animation.applyAfterActivityStarted(abilitySlice);
        }

    }

    public void finish(TransitionAnimation animation) {
        if (animation != null) {
            animation.applyBeforeActivityFinished(abilitySlice);
        }

        if (abilitySlice == null) {
            return;
        }
        int index = AbilityStack.getInstance().isContainAbility(abilitySlice);
        if (index == -1) {
            abilitySlice = AbilityStack.getInstance().peek();
            finish(animation);
        }

        if (abilitySlice instanceof FractionAbility) {
            FractionStack.getInstance(((FractionAbility) abilitySlice).getFractionManager()).removeAllFragmentByGroup(abilitySlice.toString());
        }
        abilitySlice.terminateAbility();
        AbilityStack.getInstance().onAbilityStop(abilitySlice);
        if (animation != null) {
            animation.applyAfterActivityFinished(abilitySlice);
        }
    }
}
